package cn.xz.zgsg.dialog

import android.view.Gravity
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import cn.xz.zgsg.R
import cn.xz.zgsg.base.BaseDialogRxFragment
import cn.xz.zgsg.util.ToastUtil

/**
 * 操作欢迎词弹窗
 * */
class AddWelComeDialog: BaseDialogRxFragment() {

    private var mClickListener: OnClickListener? = null

    private var textSize: Int = 120
    private var text: String = "请输入文字"

    private var tvOk: TextView? = null
    private var tvCancel: TextView? = null
    private var tvWelSize: TextView? = null
    private var tvTitle: TextView? = null
    private var ivAddWel: ImageView? = null
    private var ivJWel: ImageView? = null
    private var etDialogEdit: EditText? = null

    companion object {
        fun newInstance(
            textSize: Int,
            text: String,
            clickListener: OnClickListener
        ): AddWelComeDialog {
            val dialog = AddWelComeDialog()
            dialog.textSize = textSize
            dialog.text = text
            dialog.mClickListener = clickListener
            return dialog
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_add_welcome
    }

    override fun configDialog() {
        this.setDialogSize(gravity = Gravity.CENTER,width = 500)
    }

    override fun initViews() {
        dialog?.let {
            tvOk = it.findViewById(R.id.tv_ok)
            tvCancel = it.findViewById(R.id.tv_cancel)
            tvWelSize = it.findViewById(R.id.tv_wel_size)
            ivAddWel = it.findViewById(R.id.iv_add_wel)
            ivJWel = it.findViewById(R.id.iv_j_wel)
            etDialogEdit = it.findViewById(R.id.et_dialog_edit)
            tvTitle = it.findViewById(R.id.tv_title)
        }

        //点击外部不隐藏
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
        //数据
        initData()
        //点击事件
        initEvent()
    }

    private fun initData(){
        if (tvWelSize != null){
            tvWelSize!!.text = textSize.toString()
        }
        if (etDialogEdit != null){
            if (text.contains("请输入文字")){
                tvTitle?.text = "请输入需要添加的欢迎词"
                etDialogEdit!!.hint = text
            }else{
                tvTitle?.text = "编辑当前欢迎词"
                etDialogEdit!!.setText(text)
            }
        }
    }

    private fun initEvent(){
        tvOk?.setOnClickListener {
            if (tvWelSize!= null && etDialogEdit != null && etDialogEdit!!.text.toString().isNotEmpty()){
                mClickListener?.onYes(Integer.parseInt(tvWelSize!!.text.toString()),etDialogEdit!!.text.toString())
                dismiss()
            }else{
                ToastUtil.show("内容为空")
            }
        }
        tvCancel?.setOnClickListener {
            dismiss()
        }
        ivAddWel?.setOnClickListener {
            val mS = Integer.parseInt(tvWelSize!!.text.toString())
            tvWelSize?.text = (mS + 5).toString()
        }
        ivJWel?.setOnClickListener {
            val mS = Integer.parseInt(tvWelSize!!.text.toString())
            if (mS > 5){
                tvWelSize?.text = (mS - 5).toString()
            }
        }
    }


    interface OnClickListener {
        fun onYes(i: Int,s: String)
    }

}